package com.kaifamiao.demo.d0906;

import java.sql.*;

public class Demo01 {

    public static void main(String[] args) {
        String str = "2022-09-01";

        Date date = Date.valueOf(str);

        System.out.println(date);

        // 注册驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/kfm";
        String sql = "INSERT INTO EMP (empno, ename, hiredate) values(?, ?, ?)";
        try( // 创建连接
             Connection conn = DriverManager.getConnection(url, "root", "");
             PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ) {

            // 设置参数
            preparedStatement.setInt(1, 1234);
            preparedStatement.setString(2,"abc");
            preparedStatement.setObject(3, date);

            // 执行
            System.out.println(preparedStatement.executeUpdate());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
